﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SaaS.Core.Authorization;
using SaaS.Model.Entity.Blogs;
using Web.System.Services.Blogs;
using SaaS.Model.Dto.Blogs;
using XCore.Authorization;

namespace Web.System.Controllers.Blogs
{
    [Route("api/blog/[controller]/[action]")]
    [ApiController]
    public class MenuController : SystemControllerBase
    {
        private readonly MenuAppService _menuApplicationService;
        public MenuController(MenuAppService menuApplicationService)
        {
            _menuApplicationService = menuApplicationService;
        }
        /// <summary>
        /// 带子级
        /// </summary>
        /// <returns></returns>
        [HttpGet, Permission(PermissionNames.Blog_Menu)]
        public ActionResult List()
        {
            var query = SDC.BlogMenus.OrderByDescending(c => c.Sort);
            var menus = ObjectMapper.Map<List<MenuDto>>(query);
            return Ok(menus);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[HttpPost, Authorization(PermissionNames.Blog_Menu_Create)]
        //public ActionResult Save(MenuInput input)
        //{
        //    return Ok(_menuApplicationService.CreateOrEdit<BlogMenu, MenuInput>(input));
        //}
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpPost, Permission(PermissionNames.Blog_Menu)]
        public ActionResult Sort(int id, int value)
        {
            return Ok(_menuApplicationService.Sort(id, value));
        }
        /// <summary>
        /// 开关可见
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Permission(PermissionNames.Blog_Menu)]
        public ActionResult Visble(int id)
        {
            return Ok(_menuApplicationService.Visble(id));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[HttpDelete("{id}"), Authorization(PermissionNames.Blog_Menu_Delete)]
        //public ActionResult Delete(int id)
        //{
        //    return Ok(_menuApplicationService.Delete<BlogMenu>(id));
        //}
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[HttpGet("{id}"), Authorization(PermissionNames.Blog_Menu)]
        //public ActionResult Info(int id)
        //{
        //    return Ok(SDC.BlogMenus.FirstOrDefault(c => c.Id == id) ?? new BlogMenu());
        //}
        /// <summary>
        /// 获取一级菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet, Permission(PermissionNames.Blog_Menu)]
        public ActionResult Dictionary()
        {
            return Ok(SDC.BlogMenus.Where(c => c.IsVisble && c.ParentId == null).ToDictionary(c => c.Id, c => c.Title));
        }
    }
}